package com.elinkway.infinitemovies.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.adapter.AlbumGridViewAdapter;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.FeedbackQuestionBean;
import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bean.ReportSubmitBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.FeedbackQurstionParser;
import com.elinkway.infinitemovies.http.requesttask.RequestFeedbackQuestionTask;
import com.elinkway.infinitemovies.http.requesttask.UploadUserFeedbackTask;
import com.elinkway.infinitemovies.ui.activity.AdviseActivity;
import com.elinkway.infinitemovies.ui.activity.FeedbackSubmitActivity;
import com.elinkway.infinitemovies.ui.activity.ReportActivity;
import com.elinkway.infinitemovies.utils.BitmapUtils;
import com.elinkway.infinitemovies.utils.LogCat;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.le123.ysdq.R;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by zhaopeipei on 2016/6/13.
 */
public class UserFeedBackFragment extends BaseFragment implements View.OnClickListener, AbsListView.OnItemClickListener{
    private static final String TAG = "UserFeedBackFragment";

    private ListView mListView;
    private QuestionItemAdapter mQuestionItemAdapter;
    private ArrayList<FeedbackQuestionBean.QuestionItem> mList;
    private ReportActivity masterActivity;

    private View mParentView;

    private RelativeLayout mPlayProblemRelativeLayout;
    private TextView mPlayProblemTextView;
    private ImageView mPlayProblemImageView;

    private RelativeLayout mDownloadProblemRelativeLayout;
    private TextView mDownloadProblemTextView;
    private ImageView mDownloadProblemImageView;

    private RelativeLayout mOtherProblemRelativeLayout;
    private TextView mOtherProblemTextView;
    private ImageView mOtherProblemImageView;

    private EditText mUserEditTextView;

    private String mCurrentProblemType;

    private boolean isCreatedView;

    @Override
    public void onAttach(Activity activity) {
        masterActivity = (ReportActivity) activity;
        super.onAttach(activity);
        super.setFragmentClassName(TAG);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtils.e("UserFeedBackFragment", "UserFeedBackFragment onCreateView");
//        mCurrentProblemType = "";
        mParentView = inflater.inflate(R.layout.fragment_user_feedback,
                container, false);
        initView(mParentView);
//        initData();
        requestQustionList();
//        isCreatedView = true;
        return mParentView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtils.e(TAG, "setUserVisibleHint " + isVisibleToUser + " " + isCreatedView);
        ReportActivity tActivity = (ReportActivity) getActivity();
        if (tActivity != null) {
            tActivity.changeSubmitButtonState(false);
        }
//        if (!isCreatedView) {
//            return;
//        }
//        if (mUserEditTextView != null && !TextUtils.isEmpty(mUserEditTextView.getText().toString())) {
//            masterActivity.notification(UserFeedBackFragment.this, true, 0);
//        } else if ((mUserEditTextView == null || TextUtils.isEmpty(mUserEditTextView.getText().toString())
//            && !TextUtils.isEmpty(mCurrentProblemType))){
//            masterActivity.notification(UserFeedBackFragment.this, false, 1);
//        } else if (TextUtils.isEmpty(mCurrentProblemType) && (mUserEditTextView != null &&
//                !TextUtils.isEmpty(mUserEditTextView.getText().toString()))) {
//            masterActivity.notification(UserFeedBackFragment.this, false, 2);
//        } else {
//            masterActivity.notification(UserFeedBackFragment.this, false, 4);
//        }
    }

    public void initView(View parentView) {
        mListView = (ListView) parentView.findViewById(R.id.question_list);
        mQuestionItemAdapter = new QuestionItemAdapter(getActivity());
        mListView.setAdapter(mQuestionItemAdapter);
        mListView.setOnItemClickListener(this);
//        mPlayProblemRelativeLayout = (RelativeLayout) parentView.findViewById(R.id.rl_play_problem);
//        mPlayProblemTextView = (TextView) parentView.findViewById(R.id.tv_play_problem);
//        mPlayProblemImageView = (ImageView) parentView.findViewById(R.id.iv_play_problem);
//        mDownloadProblemRelativeLayout = (RelativeLayout) parentView.findViewById(R.id.rl_download_problem);
//        mDownloadProblemTextView = (TextView) parentView.findViewById(R.id.tv_download_problem);
//        mDownloadProblemImageView = (ImageView) parentView.findViewById(R.id.iv_download_problem);
//        mOtherProblemRelativeLayout = (RelativeLayout) parentView.findViewById(R.id.rl_other_problem);
//        mOtherProblemTextView = (TextView) parentView.findViewById(R.id.tv_other_problem);
//        mOtherProblemImageView = (ImageView) parentView.findViewById(R.id.iv_other_problem);
//        mUserEditTextView = (EditText) parentView.findViewById(R.id.et_user_feedback_text);
//        mUserEditTextView.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                LogCat.e(TAG, "beforeTextChanged " + s + " " + start + " " + count + " " + after);
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                LogCat.e(TAG, "onTextChanged " + s + " " + start + " " + before + " " + count);
//                changeSubmitStatus(s.toString(), mCurrentProblemType);
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                LogCat.e(TAG, "afterTextChanged " + mUserEditTextView.getSelectionStart() + " " +
//                        mUserEditTextView.getSelectionEnd());
//
//            }
//        });
//        mPlayProblemRelativeLayout.setOnClickListener(this);
//        mDownloadProblemRelativeLayout.setOnClickListener(this);
//        mOtherProblemRelativeLayout.setOnClickListener(this);
    }

    public void initData() {
//        LogUtils.e(TAG, "initData " + BitmapUtils.feedbackType);
//        if (TextUtils.isEmpty(masterActivity.getAid()) ||
//                !masterActivity.getAid().equals(BitmapUtils.lastAid)) {
//            return;
//        }
//        if (BitmapUtils.feedbackType > 0) {
//            switch (BitmapUtils.feedbackType) {
//                case 1:
//                    changeProblemText(1);
//                    break;
//                case 2:
//                    changeProblemText(2);
//                    break;
//                case 3:
//                    changeProblemText(3);
//                    break;
//
//            }
//        }
//        if (!TextUtils.isEmpty(BitmapUtils.feedbackDescription) && mUserEditTextView !=null) {
//            mUserEditTextView.setText(BitmapUtils.feedbackDescription);
//        }
//        changeSubmitStatus(BitmapUtils.feedbackDescription, mCurrentProblemType);
    }

    public void requestQustionList() {
        new RequestFeedbackQuestionTask(getActivity(), new RequestResultListener<FeedbackQuestionBean>() {

            @Override
            public boolean onRequestFailed() {
                return false;
            }

            @Override
            public void onRequestSuccess(int updateId, FeedbackQuestionBean result) {
                if (result == null) {
                    return;
                }
                mList = result.category;
                if (mQuestionItemAdapter != null) {
                    mQuestionItemAdapter.updateList(mList);
                }

            }

            @Override
            public void onPreRequest() {

            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.rl_play_problem:
//                changeProblemText(1);
//                changeSubmitStatus(mUserEditTextView.getText().toString(), mCurrentProblemType);
//                break;
//            case R.id.rl_download_problem:
//                changeProblemText(2);
//                changeSubmitStatus(mUserEditTextView.getText().toString(), mCurrentProblemType);
//                break;
//            case R.id.rl_other_problem:
//                changeProblemText(3);
//                changeSubmitStatus(mUserEditTextView.getText().toString(), mCurrentProblemType);
//                break;
        }

    }

//    public void changeProblemText(int i) {
//        if (i <= 0) {
//            return;
//        }
//        switch (i) {
//            case 1:
//                mCurrentProblemType = "1";
//                masterActivity.notification(UserFeedBackFragment.this, false, 1);
//                mPlayProblemTextView.setTextColor(getResources().getColor(R.color.white));
//                mPlayProblemImageView.setVisibility(View.VISIBLE);
//                mPlayProblemTextView.setBackgroundColor(getResources().getColor(R.color.color_ff3599f9));
//                mDownloadProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mDownloadProblemImageView.setVisibility(View.GONE);
//                mDownloadProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                mOtherProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mOtherProblemImageView.setVisibility(View.GONE);
//                mOtherProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                break;
//            case 2:
//                mCurrentProblemType = "2";
//                masterActivity.notification(UserFeedBackFragment.this, false, 1);
//                mPlayProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mPlayProblemImageView.setVisibility(View.GONE);
//                mPlayProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                mDownloadProblemTextView.setTextColor(getResources().getColor(R.color.white));
//                mDownloadProblemImageView.setVisibility(View.VISIBLE);
//                mDownloadProblemTextView.setBackgroundColor(getResources().getColor(R.color.color_ff3599f9));
//                mOtherProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mOtherProblemImageView.setVisibility(View.GONE);
//                mOtherProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                break;
//            case 3:
//                mCurrentProblemType = "3";
//                masterActivity.notification(UserFeedBackFragment.this, false, 1);
//                mPlayProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mPlayProblemImageView.setVisibility(View.GONE);
//                mPlayProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                mDownloadProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
//                mDownloadProblemImageView.setVisibility(View.GONE);
//                mDownloadProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
//                mOtherProblemTextView.setTextColor(getResources().getColor(R.color.white));
//                mOtherProblemImageView.setVisibility(View.VISIBLE);
//                mOtherProblemTextView.setBackgroundColor(getResources().getColor(R.color.color_ff3599f9));
//                break;
//        }
//    }
//
//    public void changeSubmitStatus(String description, String currentProblemType) {
//        if (!TextUtils.isEmpty(description) && !TextUtils.isEmpty(currentProblemType)) {
//            // TODO 提交按钮变色,可以提交
//            masterActivity.notification(UserFeedBackFragment.this, true, 0);
//        } else if (TextUtils.isEmpty(description) && !TextUtils.isEmpty(currentProblemType)) {
//            masterActivity.notification(UserFeedBackFragment.this, false, 1);
//        } else if (TextUtils.isEmpty(currentProblemType) && !TextUtils.isEmpty(description)) {
//            masterActivity.notification(UserFeedBackFragment.this, false, 2);
//        } else  {
//            masterActivity.notification(UserFeedBackFragment.this, false, 4);
//        }
//    }
//
//    /**
//      ** 提交内容
//     */
    public void submit() {
        LogCat.e(TAG, "submit " + masterActivity.getAid() + " " + mCurrentProblemType);
        if (mUserEditTextView != null && !TextUtils.isEmpty(mUserEditTextView.getText().toString())) {
            UploadUserFeedbackTask uploadUserFeedbackTask = new UploadUserFeedbackTask(getActivity(),
                    mCurrentProblemType, masterActivity.getAid(), mUserEditTextView.getText().toString());
            uploadUserFeedbackTask.setListener(new RequestResultListener<ReportSubmitBean>() {

                @Override
                public void onRequestSuccess(int updateId, ReportSubmitBean result) {
                    if (result != null && "200".equals(result.getCode())) {
                        if (mUserEditTextView != null) {
                            mUserEditTextView.setText("");
                            mCurrentProblemType = "";
                            masterActivity.notification(UserFeedBackFragment.this, false, 4);
                            ToastUtil.showShortToast(getActivity(), R.string.feedback_succeed);
                            BitmapUtils.feedbackType = 0;
                            BitmapUtils.feedbackDescription = "";
                            mPlayProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
                            mPlayProblemImageView.setVisibility(View.GONE);
                            mPlayProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
                            mDownloadProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
                            mDownloadProblemImageView.setVisibility(View.GONE);
                            mDownloadProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
                            mOtherProblemTextView.setTextColor(getResources().getColor(R.color.color_ff666666));
                            mOtherProblemImageView.setVisibility(View.GONE);
                            mOtherProblemTextView.setBackgroundColor(getResources().getColor(R.color.white));
                        }
                    } else if (result == null || !"200".equals(result.getCode())) {
                        ToastUtil.showShortToast(getActivity(), R.string.feedback_failed);
                        masterActivity.notification(UserFeedBackFragment.this, true, 0);
                    }
                }

                @Override
                public boolean onRequestFailed() {
                    ToastUtil.showShortToast(getActivity(), R.string.feedback_failed);
                    masterActivity.notification(UserFeedBackFragment.this, true, 0);
                    return false;
                }

                @Override
                public void onPreRequest() {
                }
            });
            uploadUserFeedbackTask.start();

        }
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.e(TAG, "onStop");
        if (mUserEditTextView != null && !TextUtils.isEmpty(mUserEditTextView.getText().toString())) {
            BitmapUtils.feedbackDescription = mUserEditTextView.getText().toString();
            LogUtils.e(TAG, "onStop " + mUserEditTextView.getText().toString());
            BitmapUtils.lastAid = masterActivity.getAid();
        }
        if (!TextUtils.isEmpty(mCurrentProblemType)) {
            BitmapUtils.feedbackType = Integer.parseInt(mCurrentProblemType);
            BitmapUtils.lastAid = masterActivity.getAid();
            LogUtils.e(TAG, "onStop " + BitmapUtils.feedbackType);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.e(TAG, "onDestroyView");
        if (mUserEditTextView != null && !TextUtils.isEmpty(mUserEditTextView.getText().toString())) {
            BitmapUtils.feedbackDescription = mUserEditTextView.getText().toString();
            LogUtils.e(TAG, "onDestroyView " + mUserEditTextView.getText().toString());
            BitmapUtils.lastAid = masterActivity.getAid();
        }
        if (!TextUtils.isEmpty(mCurrentProblemType)) {
            BitmapUtils.feedbackType = Integer.parseInt(mCurrentProblemType);
            BitmapUtils.lastAid = masterActivity.getAid();
            LogUtils.e(TAG, "onDestroyView " + BitmapUtils.feedbackType);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        FeedbackQuestionBean.QuestionItem questionItem = mList.get(position);
        if (questionItem.subcategory == null || questionItem.subcategory.size() == 0) {
            AdviseActivity.launchActivity(getActivity(), masterActivity.getAid(), questionItem.id);
            return;
        }
        FeedbackSubmitActivity.launchActivity(getActivity(), masterActivity.getAid(), masterActivity.getSite(), questionItem.id, questionItem.subcategory);
    }

    public class QuestionItemAdapter extends BaseAdapter {

        Context mContext;
        private List<FeedbackQuestionBean.QuestionItem> mData = new ArrayList<>();

        public QuestionItemAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.question_item, null);
                viewHolder = new ViewHolder();
                viewHolder.question = (TextView) convertView.findViewById(R.id.question);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder)convertView.getTag();
            }

            FeedbackQuestionBean.QuestionItem questionItem = mData.get(position);
            if (questionItem != null) {
                viewHolder.question.setText(questionItem.name);
            }

            return convertView;
        }

        public void updateList(List<FeedbackQuestionBean.QuestionItem> data) {
            mData = data;
            notifyDataSetChanged();
        }
    }

    private class ViewHolder {
        TextView question;
    }

}
